import Vue from "vue";
import VueRouter from "vue-router";

const Home = () => import('../views/Home.vue')
const Login = () => import('../views/Login.vue')

Vue.use(VueRouter);

const routes = [
  {
    path:'',
    redirect:'/home'
  },
  {
    path: "/home",
    component: Home,
    meta: {
      title: '首页'
    }
  },
  {
    path: "/login",
    component: Login,
    meta: {
      title: '登录'
    }
  },
];

const router = new VueRouter({
  routes,
  mode:'hash',
});

// 导航守卫
router.beforeEach((to, from, next) => {
  let token = localStorage.getItem('Authorization');
  // console.log('to=>',to)
  // console.log('from=>',from)
  if (to.path === '/login') {
    if (!token) {
      next();
    } else {
      next(false);
    }
    
  } else {
    if (token === null || token === '') {
      next('/login');
    } else {
      next();
    }
  }
});
export default router;